package fr.istic.linkedcv.client.presentation;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.VerticalPanel;

import fr.istic.linkedcv.client.PersonneCli;
import fr.istic.linkedcv.client.services.PersonneService;
import fr.istic.linkedcv.client.services.PersonneServiceAsync;

public class Personne extends HorizontalPanel {
	private final PersonneServiceAsync personneServiceAsync = GWT.create(PersonneService.class);
	private Ui ui;
	public Personne(PersonneCli currentPersonne, PersonneCli personneCli, Ui ui){
		build(personneCli, ui);
		
		if(!hasFriend(currentPersonne, personneCli)){									
			Button addAmi = new Button("ajouter ami");
			addAmi.addClickHandler(addFriend(personneCli.getLogin()));
			add(addAmi);
		}
	}

	private void build(PersonneCli personne, Ui ui) {
		addStyleName("element-list");
		this.ui = ui;
		VerticalPanel vPanel = new VerticalPanel();
		vPanel.add(new HTML("<div>" + personne.getLogin() +"</div>"));
		Hyperlink lienCv = new Hyperlink("Voir le cv","voir_cv-"+personne.getLogin());
		vPanel.add(lienCv);		
		add(vPanel);		
	}
	
	
	public Personne(PersonneCli personne, Ui ui) {
		build(personne, ui);
		Button sendMessage = new Button("envoyer un message");
		sendMessage.addClickHandler(sendMessage(personne.getLogin()));
		add(sendMessage);
	
		Button deleteAmi = new Button("Supprimer ami");
		deleteAmi.addClickHandler(deleteFriend(personne.getLogin()));
		add(deleteAmi);
	}

	private ClickHandler sendMessage(final String login) {
		ClickHandler res = new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				ui.addHistoryItem("message_form-"+login);
				
			}
		};
		return res;
	}

	private ClickHandler deleteFriend(final String login) {
		ClickHandler res = new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				personneServiceAsync.deleteFriend(login, new AsyncCallback<Boolean>() {
					
					@Override
					public void onSuccess(Boolean result) {
						if(result){
							ui.fireCurrentHistoryState();
						}else{
							Window.alert("Echec lors de la suppression de l'ami");
						}
						
					}
					
					@Override
					public void onFailure(Throwable caught) {
						
					}
				});
				
			}
		};
		return res;
	}

	protected ClickHandler addFriend(final String identifiant) {
		ClickHandler res = new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				personneServiceAsync.addFriend(identifiant, new AsyncCallback<Boolean>() {
					
					@Override
					public void onSuccess(Boolean result) {
						if(result){
							ui.addHistoryItem("liste_amis");
						}else{
							Window.alert("Echec lors de l'ajout de l'ami");
						}
						
					}
					
					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}
				});
				
			}
		};
		return res;
	}

	private boolean hasFriend(PersonneCli currentPersonne, PersonneCli personneCli){
		boolean res = false;
		for(String nameCurrentPersonne : currentPersonne.getListeAmis()){
			if(nameCurrentPersonne.equals(personneCli.getLogin())){
				return true;
			}
		}
		return  res;
	}
}
